
<?php echo "<?php\n"; 
 echo '/**
 * Genarator by KKDGen v'.$version.'
 * @author KKDeveloeper
 * @email Dixonsatit@gmail.com
 * @link www.ikkdev.com,www.kkdeveloper.com
 * Controller for extjs : '. $modelClass.'Controller
 */';

 foreach ($columns as $value)
{
    $pk = $value->name;
    break;
}
?>

class <?php echo $modelClass;?>Controller extends Controller
{

    public $offset;
    public $limit;
    public $group;
    public $sort;

    public function init()
    {
        $this->offset = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
        $this->limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 50;
    }

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
                // 'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array(),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('index', 'create', 'update', 'delete'),
                'users' => array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions' => array('admin', 'delete'),
                'users' => array('admin'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    /**
     * Lists all models.
     */
    public function actionIndex()
    {
        
          //Query with ar============================================
          $results = array();
          $keyword = isset($_GET['query']) ? $_GET['query'] : '';
          $criteria = new CDbCriteria();
          $criteria->compare('<?php echo $pk;?>', $keyword, true);
          $criteria->offset = $this->offset;
          $criteria->limit = $this->limit;
           Services::getAR($criteria,'<?php echo $modelClass?>');

         
        /*
          //Query with QueryBuider=======================================
           $keyword = isset($_GET['query']) ? $_GET['query'] : '';
          // escape % and _ characters
          $keyword = strtr($keyword, array('%' => '\%', '_' => '\_'));
          //where
          $criteria = array(
          'select' => array('*'),
          'from' => '<?php echo $tableName; ?>',
          'where' => array('like', '<?php echo $pk;?>', '%' . $keyword . '%'),
          'offset' => $this->offset,
          'limit' => $this->limit
          );
          Services::get($criteria); */
        /*
        //====Query with Sql ======================================
         $keyword = isset($_GET['query']) ? $_GET['query'] : '';
        // escape % and _ characters
        $keyword = strtr($keyword, array('%' => '\%', '_' => '\_'));
        $sql = ' select * from <?php echo  $tableName; ?>';
        $where = " where <?php echo $pk;?> like '%{$keyword}%' ";
        $sqlCount = ' select count(*) from <?php echo  $tableName; ?> ';
        $limit = ' limit ' . $this->offset . ',' . $this->limit;
        Services::getDAO($sql,$sqlCount, $where, $limit);*/
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate($_dc)
    {
        $model = new <?php echo $modelClass; ?>;
        $model->attributes = Services::parseRequest();
        if ($model->save())
        {
            $success = TRUE;
        }
        else
        {
            $success = FALSE;
        }
        Services::response($model->attributes, 0, 'Create Record..', $success);
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate()
    {
        $value = Services::parseRequest();
        $model = $this->loadModel($value['id']);
        if ($model)
        {
            $model->attributes = $value;
            if ($model->save())
                $success = TRUE;
            else
                $success = FALSE;
        }
        Services::response($model, 0, 'Update Record..', $success);
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete()
    {
        $value = array();
        $value = Services::parseRequest();
        if ($value['id'])
        {
            $model = <?php echo $modelClass; ?>::model()->findByPk($value['id']);
            if ($model->delete())
                $success = TRUE;
            else
                $success = FALSE;
            $msg = 'Derete Record..';
        }
        else
        {
            foreach ($value as $v)
            {
                $model = <?php echo $modelClass; ?>::model()->findByPk($v['id']);
                if ($model->delete())
                    $success = TRUE;
                else
                    $success = FALSE;
            }
            $msg = "Derete " . count($value) . " Record";
        }
        Services::response($value, 0, $msg, $success);
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id)
    {
        $model = <?php echo $modelClass; ?>::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}
